package pl.pelcra.cesar.spider.data;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import pl.pelcra.cesar.spider.webcrawler.CSWebURL;

/**
 * Class stores map languages and links for one page
 * 
 * @author margielewski
 * 
 */
public class MapLangUrl implements Comparable<MapLangUrl>,
		Map<String, CSWebURL> {

	public static int i = 0;
	private int id = 0;

	private final TreeMap<String, CSWebURL> map = new TreeMap<String, CSWebURL>();

	public MapLangUrl() {
		this.id = ++i;
	}

	@Override
	public void clear() {
		this.map.clear();
	}

	@Override
	public int compareTo(MapLangUrl mapLangUrl) {
		if (this.map.size() == mapLangUrl.size()) {
			int compare = 0;
			for (String lang : this.map.keySet()) {
				compare = this.map.get(lang).compareTo(mapLangUrl.get(lang));
				if (compare != 0) {
					return compare;
				}
			}
			return 0;
		}
		return Integer.valueOf(this.map.size()).compareTo(
				Integer.valueOf(mapLangUrl.size()));
	}

	@Override
	public boolean containsKey(Object language) {
		return this.map.containsKey(language);
	}

	@Override
	public boolean containsValue(Object csWebURL) {
		return this.map.containsValue(csWebURL);
	}

	@Override
	public Set<java.util.Map.Entry<String, CSWebURL>> entrySet() {
		return this.map.entrySet();
	}

	@Override
	public CSWebURL get(Object language) {
		return this.map.get(language);
	}

	/**
	 * return id of page
	 * 
	 * @return
	 */
	public int getId() {
		return this.id;
	}

	@Override
	public boolean isEmpty() {
		return this.map.isEmpty();
	}

	@Override
	public Set<String> keySet() {
		return this.map.keySet();
	}

	@Override
	public CSWebURL put(String language, CSWebURL csWebURL) {
		if (!this.map.containsKey(language)) {
			return this.map.put(language, csWebURL);
		}
		return null;
	}

	@Override
	public void putAll(Map<? extends String, ? extends CSWebURL> mapPageURL) {
		this.map.putAll(mapPageURL);
	}

	@Override
	public CSWebURL remove(Object language) {
		return this.map.remove(language);
	}

	@Override
	public int size() {
		return this.map.size();
	}

	@Override
	public Collection<CSWebURL> values() {
		return this.map.values();
	}
}
